package checkDetail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import general.BaseBean;

public class WorkflowUrgerdetailConditionCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowUrgerdetailConditionCheck checkData start");
        
        String dbType = recordSet.getDBType();
        //定义查询sql
        String checkSql = "";
        //判断数据库类型
        if("sqlserver".equals((dbType))) {
            checkSql = "select id as udId, conditions from workflow_urgerdetail where conditions is not null and conditions <> ''";
        }else{
            checkSql = "select id as udId, conditions from workflow_urgerdetail where conditions is not null";
        }
        
        recordSet.executeSql(checkSql);
        List resultList = new ArrayList();
        String result = "";
        while(recordSet.next()){
            //如果有值则检测conditions是否满足规则
            String udId = recordSet.getString("udId");
            String conditions = recordSet.getString("conditions");
            if(conditions.indexOf(".") >= 0  || conditions.indexOf("(") >= 0){
                continue;
            }
            
            if(conditions.contains(",")){
                String[] conditionArr = conditions.split(",");
                for (String condition : conditionArr) {
                    if(!StringUtils.isNumeric(condition) || "".equals(condition)){
                        Map resultMap = new HashMap<String,String>();
                        resultMap.put("showMessage","workflow_urgerdetail表中id为" + udId + "的condition字段：" + conditions + "有问题，请单条删除修复！");
                        resultMap.put("udId",udId);
                        resultMap.put("buttonname","删除");
                        resultList.add(resultMap);
                        break;
                    }
                }
            }else{
                if(!StringUtils.isNumeric(conditions)){
                    Map resultMap = new HashMap<String,String>();
                    resultMap.put("showMessage","workflow_urgerdetail表中id为" + udId + "的condition字段：" + conditions + "有问题，请单条删除修复！");
                    resultMap.put("udId",udId);
                    resultMap.put("buttonname","删除");
                    resultList.add(resultMap);
                }
            }
        }
        if(resultList.size() == 0){
            result = resultOk;
            resultList.add("没有检测到workflow_urgerdetail表中condition字段存在问题。");
        }else{
            result = resultNeedFix;
        }
        (new BaseBean()).writeLog("WorkflowCustompageCheck checkData end");
        return formatReturnJsonCheck(result, resultList, this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowUrgerdetailConditionCheck  fixData start");
        
        String divid = (String)dataMap.get("divid");
        List resultList = new ArrayList();
        Map resultMap = new HashMap<String,String>();
        String result = resultOk;
        if(dataMap.containsKey("udId")  && !"".equals(dataMap.get("udId"))){
            String udId = (String)dataMap.get("udId");
            if(!StringUtils.isNumeric(udId)){
                (new BaseBean()).writeLog("WorkflowUrgerdetailConditionCheck udId is not isNumeric");
                return checkData(new HashMap());
            }
            String updateSql = "update workflow_urgerdetail set conditions = '' where id = " + Integer.parseInt(udId);
            recordSet.executeUpdate(updateSql);
            resultMap.put("result",resultOk);
            resultMap.put("fixMessage","修复成功");
            resultMap.put("divid",divid);
            resultList.add(resultMap);
            (new BaseBean()).writeLog("WorkflowUrgerdetailConditionCheck  fixData end");
            return formatReturnJsonFix(result, resultList,this);
        }
        (new BaseBean()).writeLog("WorkflowUrgerdetailConditionCheck  fixData end");
        return checkData(new HashMap());
    }

    @Override
    public String getClassDesc() {
        return "检测workflow_urgerdetail表中condition字段是否存在问题";
    }

    @Override
    public String getClassName() {
        return "workflow_urgerdetail表中condition字段检测";
    }
    

}
